package com.java1234.jxc.config;


import com.fasterxml.jackson.databind.util.JSONPObject;
import com.google.gson.JsonObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.json.JSONException;
import org.json.JSONObject;
import org.nutz.mvc.ActionContext;
import org.nutz.mvc.view.ServerRedirectView;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class MyExceptionResolver implements HandlerExceptionResolver{

    private final Log logger = LogFactory.getLog(this.getClass());

    protected String loginUri = "/authority/noLoginJSON";

    protected String noAuthUri = "/authority/noPermissionJSON";
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object o, Exception ex) {

        MappingJackson2JsonView mappingJackson2JsonView = new MappingJackson2JsonView();
       // ModelAndView mv=new ModelAndView("/auth/error");
       Map<String,Object> map = new HashMap<>();


        try {


            if (ex instanceof UnauthenticatedException){    // 登录失效
                request.getRequestDispatcher("/auth/error/登录失效!").forward(request,response);
            }else if (ex instanceof UnauthorizedException){  // 没有权限
                request.getRequestDispatcher("/auth/error/没有权限!").forward(request,response);
            }else{
                request.getRequestDispatcher("/auth/error/登录失效!").forward(request,response);
            }



        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        return null;
    }








}
